package edu.jptest.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import edu.jptest.bean.QuestionContent;
import edu.jptest.util.GetDataBaseConnection;

public class QuestionContentDAO {

	// 插入题干
	public boolean insertQuestionContent(QuestionContent questioncontent) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "insert into question_content(question_content) values(?)";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, questioncontent.getQuestionContent());
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 删除题干
	public boolean deleteQuestionContent(int questionContentId) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "delete from question_content where question_content_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, questionContentId);
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 修改题干
	public boolean updateQuestionContent(QuestionContent questioncontent) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "update question_content set question_content=? where question_content_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, questioncontent.getQuestionContent());
			pstm.setInt(2, questioncontent.getQuestionContentId());
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 通过题干id，查询题干对象
	public QuestionContent getQuestionContentByQuestionContentId(
			int questionContentId) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		QuestionContent questionContent = new QuestionContent();
		PreparedStatement pstm = null;
		try {
			String sql = "select * from question_content where question_content_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, questionContentId);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				questionContent.setQuestionContentId(resultSet.getInt(1));
				questionContent.setQuestionContent(resultSet.getString(2));
				return questionContent;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 通过题干内容，查询题干ID
	public int getQuestionContentIdByContent(String question_content) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		try {
			String sql = "select question_content_id from question_content where question_content=?";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, question_content);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				int contentId = resultSet.getInt(1);
				return contentId;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return 0;
	}

}
